
import 'package:flutter/material.dart';
import 'package:flutter_data_json_app/entity/request_bean.dart';

Widget getBaseView(Function normalWidget,RequestBean request,Function onTryAgain){
  if(null==request){
    return _getEmpty(onTryAgain);
  }else{
    if(request.code==0){
      if(request.data.dataIsEmpty()){
        return _getEmpty(onTryAgain);
      }else{
        return normalWidget();
      }
    }else{
      return _getError(onTryAgain,errText: request.msg);
    }
  }
}

Widget _getError(Function onTryAgain,{String errText:'未知错误'}){
  return Container(
    width: double.infinity,
    height: double.infinity,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Icon(Icons.error),
        Text(errText),
        FlatButton(
          onPressed: ()=>onTryAgain(),
          child: Text('点击重试'),
        )
      ],
    ),
  );
}

Widget _getEmpty(Function onTryAgain,{String emptyText:'没有数据哦'}){
  return Container(
    width: double.infinity,
    height: double.infinity,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Icon(Icons.error),
        Text(emptyText),
        FlatButton(
          onPressed: ()=>onTryAgain(),
          child: Text('点击刷新'),
        )
      ],
    ),
  );
}